package com.peng.rpc.registry;

import com.peng.rpc.util.NacosUtil;
import com.peng.rpc.util.ThreadPoolFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;

/**
 * 向jvm注册一个在关闭时销毁实例的钩子
 * @author Wonderson
 * @email wpp.ds@foxmail.com
 * @date 2022/3/7 10:04
 */
@Slf4j
public class ShutDownHook {

    /**
     * 用于测试这个类是否可以成功使用
     */
    private static final ExecutorService threadPool = ThreadPoolFactory.createDefaultThreadPool("shutdown-hook");

    public static void addShutDownHooks() {
        log.info("告知JVM关闭时注销所有的服务");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            NacosUtil.deregister();
            ThreadPoolFactory.shutdownAll();
        }));
    }

}
